/*
 * Copyright (c) 2007 jNetX.
 * http://www.jnetx.com
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * jNetX. You shall not disclose such Confidential Information and
 * shall use it only in accordance with the terms of the license
 * agreement you entered into with jNetX.
 *
 * $Id$
 */
package com.jnetx.javax.sip.util;

import junit.framework.TestCase;

import java.text.ParseException;

/**
 * @author <a href="mailto:dparhonin@jnetx.ru">Dmitry Parhonin</a>
 * @version $Revision$
 */
public class ByteArrayUtilTest extends TestCase {
    public ByteArrayUtilTest(String name) {
        super(name);
    }

    public void test() {
        byte[] data = "This text is intentionally meanless.".getBytes();
        assertEquals("", 32, ByteArrayUtil.firstMissingBefore(data, 's', '.', 0, data.length));
    }

    public void testLastMissing() {
        byte[] data = "Thsi is   kffdg     param-param     1234.33. Go!".getBytes();
        assertEquals("", 14, ByteArrayUtil.lastMissing(data, ' ', 0, 18));
        assertEquals("", 3, ByteArrayUtil.lastMissing(data, ' ', 0, 4));
    }

    public void testEquals() {
        byte[] data1 = "text is intentionally".getBytes();
        byte[] data2 ="is int".getBytes();
        assertTrue("", ByteArrayUtil.equals(data1, 5, 11, data2, 0, data2.length));
        assertFalse("", ByteArrayUtil.equals(data1, 6, 11, data2, 0, data2.length));
        assertFalse("", ByteArrayUtil.equals(data1, 5, 12, data2, 0, data2.length));
    }

    public void testParseInteger() {
        byte[] data="abra1295rfdabra".getBytes();
        try {
            assertEquals("", 1, ByteArrayUtil.parseInteger(data, 4, 5));
            assertEquals("", 12, ByteArrayUtil.parseInteger(data, 4, 6));
            assertEquals("", 129, ByteArrayUtil.parseInteger(data, 4, 7));
            assertEquals("", 1295, ByteArrayUtil.parseInteger(data, 4, 8));
        } catch (ParseException e) {
            fail(e.getMessage());
        }
        try {
            ByteArrayUtil.parseInteger(data, 3,7);
            fail("The exception should be thown!");
        } catch (ParseException e) {
        }
    }

    public void testStartsWith() {
        byte[] sample = "fuck".getBytes();
        byte[] data="fuck this business".getBytes();
        assertTrue("", ByteArrayUtil.startsWith(data, 0, data.length, sample, 0, sample.length));
        assertFalse("", ByteArrayUtil.startsWith(data, 1, data.length, sample, 0, sample.length));
        assertTrue("", ByteArrayUtil.startsWith(data, 0, data.length, sample, 0, 3));
    }
}
/*
 * $Log$
 */